import Vue from "vue";
import axios from "axios";
import VueAxios from "vue-axios";
import JwtService from "@/core/services/jwt.service";
import qs from "qs";
import Error from "@/view/pages/error/Error";
/**
 * 通过Axios调用HTTP请求的服务
 */
const ApiService = {
  init() {
    Vue.use(VueAxios, axios);
    Vue.axios.defaults.timeout = 5000;
    Vue.axios.defaults.headers.post["Content-Type"] =
        "application/x-www-form-urlencoded;charset=UTF-8";
    //主机
    let host = window.location.host;
    //地址
    let reg = /^localhost+/;
    //匹配到localhost访问本地服务器
    if (reg.test(host)) {
      baseUrl(true);
    } else {
      baseUrl(false);
    }
    function baseUrl(state) {
      if (state) {
        //若本地项目调试使用
        Vue.axios.defaults.baseURL = "http://127.0.0.1:8081/IHY/";
      } else {
        //后端请求地址
        Vue.axios.defaults.baseURL = "http://127.0.0.1:8081/IHY/";
      }
    }
    Vue.axios.interceptors.request.use(
        config => {
          if (config.method === "get") {
            config.data = JSON.stringify(config.data);
          } else if (config.method === "post") {
            config.data = qs.stringify(config.data);
          } else if (config.method === "put") {
            config.data = JSON.stringify(config.data);
          } else if (config.method === "update") {
            config.data = JSON.stringify(config.data);
          } else if (config.method === "delete") {
            config.data = JSON.stringify(config.data);
          }
          return config;
        },
        error => {
          return Promise.reject(error);
        }
    );
  },
  /**
   * 设置默认HTTP请求头
   */
  setHeader(key) {
    if (JwtService.getToken(key) !== "undefined") {
      this.header(JwtService.getToken(key));
    }
  },
  removeHeader() {
    delete Vue.axios.defaults.headers.common.Authorization;
  },
  header(token) {
    Vue.axios.defaults.headers.common["Authorization"] = `${token}`;
  },
  query(resource, params) {
    return Vue.axios.get(resource, params).catch(error => {
      throw new Error(`[KT] ApiService ${error}`);
    });
  },
  /**
   * 发送GET HTTP请求
   * @param resource
   * @param params
   * @returns {*}
   */
  get(resource, params) {
    return Vue.axios.get(`${resource}`, params).catch(error => {
      throw new Error(`[KT] ApiService ${error}`);
    });
  },
  /**
   * 设置POST HTTP请求
   * @param resource 资源
   * @param params 参数
   * @returns {*}
   */
  post(resource, params) {
    return Vue.axios.post(`${resource}`, params);
  },
  /**
   * 发送更新HTTP请求
   * @param resource
   * @param slug
   * @param params
   * @returns {IDBRequest<IDBValidKey> | Promise<void>}
   */
  update(resource, slug, params) {
    return Vue.axios.put(`${resource}/${slug}`, params);
  },
  /**
   * 发送PUT HTTP请求
   * @param resource
   * @param params
   * @returns {IDBRequest<IDBValidKey> | Promise<void>}
   */
  put(resource, params) {
    return Vue.axios.put(`${resource}`, params);
  },
  /**
   * 发送删除HTTP请求
   * @param resource
   * @returns {*}
   */
  delete(resource) {
    return Vue.axios.delete(resource).catch(error => {
      throw new Error(`[RWV] ApiService ${error}`);
    });
  }
};
export default ApiService;
